package com.softer.chengxin.project.dao;

import com.softer.chengxin.common.domin.Page;
import com.softer.chengxin.project.dao.provider.HousingDaoProvider;
import com.softer.chengxin.project.domin.Housing;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author Gong Zhiyang
 * @date 2018/10/16
 */
@Mapper
@Repository
public interface HousingDao {
    /**
     * 新增物业用房信息
     *
     * @param housing 物业用房信息
     * @return 结果
     */
    @Insert("INSERT INTO company_housing(project_id,room_address,room_nature,room_area,room_state,remark,room_all_area,other_area,room_path,protocol,gmt_create,gmt_modified)" +
            " VALUES(#{h.projectId},#{h.roomAddress},#{h.roomNature},#{h.roomArea},#{h.roomState},#{h.remark},#{h.roomAllArea},#{h.otherArea},#{h.roomPath},#{h.protocol},NOW(),NOW())")
    int add(@Param("h") Housing housing);

    /**
     * 查询物业用房数量
     *
     * @param housing 查询实体
     * @return 数量
     */
    @SelectProvider(type = HousingDaoProvider.class, method = "queryHousingNum")
    int queryHousingNum(@Param("housing") Housing housing);

    /**
     * 查询物业用房列表
     *
     * @param page    页面实体
     * @param housing 查询实体
     * @return 列表
     */
    @SelectProvider(type = HousingDaoProvider.class, method = "queryHousingList")
    List<Housing> queryHousingList(@Param("page") Page page, @Param("housing") Housing housing);

    /**
     * 删除物业用房信息
     *
     * @param id 物业用房Id
     * @return 结果
     */
    @Delete("delete from company_housing where id=#{id}")
    int delete(@Param("id") Integer id);

    /**
     * 查看物业用房信息
     *
     * @param id 物业用房ID
     * @return 信息
     */
    @Select("SELECT id,project_id as projectId,room_address as roomAddress,room_nature as roomNature,room_area as roomArea,room_path as roomPath," +
            "room_state as roomState,remark,room_all_area as roomAllArea,other_area as otherArea,protocol FROM company_housing WHERE id=#{id}")
    Housing detail(@Param("id") Integer id);

    /**
     * 编辑物业用房信息
     *
     * @param housing 用房信息
     * @return 结果
     */
    @Update("update company_housing set gmt_modified=now(),room_address=#{h.roomAddress},room_nature=#{h.roomNature},room_area=#{h.roomArea},room_path= #{h.roomPath}," +
            "room_state=#{h.roomState},remark=#{h.remark},room_all_area=#{h.roomAllArea},other_area=#{h.otherArea},protocol=#{h.protocol} where id=#{h.id}")
    int edit(@Param("h") Housing housing);

    /**
     * 查询所有物业用房
     *
     * @return 物业用房列表
     */
    @Select("SELECT app_id as appId, s_id as id, corp_name as companyName,proj_name as projectName,area as roomArea ,address as roomAddress,s_state as state,s_flag as flag " +
            "FROM wuyeyongfang where s_flag=0")
    List<Housing> getAllHouse();

    /**
     * 修改交换状态
     *
     * @param id aapId集合，以逗号分隔
     * @return 修改状态
     */
    @Update("update wuyeyongfang set s_flag=1 where FIND_IN_SET(app_id,#{id})")
    int updateFlag(@Param("id") String id);
}
